import { createRouter, createWebHashHistory } from 'vue-router'
import RegisterView from '@/views/account/RegisterView.vue'
import LoginView from '@/views/account/LoginView.vue'
import IndexView from '@/views/index/IndexView.vue'
import NoticeComponent from '@/components/index/NoticeComponent'
import UserCenter from '@/components/account/UserCenterComponent'
import SendNotice from '@/components/index/SendNotice'
import FriendView from '@/views/chat/FriendView'
import PublicView from '@/views/chat/PublicView'
import { useUser } from '@/store'
const routes = [
  {
      path:'/',
      redirect:'/index/notice'

  }
  ,{
    path:'/index',
    name:'index',
    component:IndexView,
    children:[{
      path:'notice',
      name:'notice',
      component:NoticeComponent,
    },
    {
      path:'personChatroom',
      name:'personChatroom',
      component: FriendView,
      meta:{ isAuth:true}
    },
    {
      path:'multipleChatroom',
      name:'multipleChatroom',
      component: PublicView,
      meta:{ isAuth:true}
    },{
      path:'sendnotice',
      name:'sendnotice',
      component:SendNotice,
      meta:{ isAuth:true ,title:"sendnotice"}
    },{
      path:'userCenter',
      name:'userCenter',
      component:UserCenter,
      meta:{ isAuth:true}
    }
  ]
  }
  ,{
    path: '/register',
    name: 'register',
    component: RegisterView
  },
  {
    path: '/login',
    name: 'login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component:LoginView
  }
]
const router = createRouter({
  history: createWebHashHistory(),
  routes
})
//全局前置路由守卫————初始化的时候被调用、每次路由切换之前被调用
router.beforeEach((to, from, next) => {
  const nowUser = useUser();
  //如果路由需要跳转
  if (to.meta.isAuth) {  //没放行的路由
    if (localStorage.getItem('token') === null ) { //没登录
      alert('游客身份无法访问,请您登录！');
       next('/login');
    } else{ //已经登录
    if(to.name === 'sendnotice' && nowUser.userType == 0 )   alert("您没有此权限");
    else  next();
  }
} else next();    //开放的路由
});

export default router
// && localStorage.getItem("data").search('nickname') === -1 